package com.shenbianyun.baiwei;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.baiwei.DTO.H5UserSignDTO;
import com.shenbianyun.bosskgpay.constant.MerInfoConstant;
import com.shenbianyun.bosskgpay.entity.RequestMessage;
import com.shenbianyun.bosskgpay.entity.ResponseMessage;
import com.shenbianyun.bosskgpay.entity.sign.UserSignQueryReqModel;
import com.shenbianyun.bosskgpay.utils.*;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

/**
 * @author ：张一凡
 * @date ：2024年02月27日 10:04:19
 * @description：百威签约：获取h5签约页面api接口，然后提交信息http内部接口
 */
public class BWUserSignserviceJ extends AbstractJavaSamplerClient {

    /**api参数开始**/
    public final String CHARSET_UTF_8 = "UTF-8";
    public final Long MER_ID = 1643004211569439L;
    public final String MER_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMhyR4I/yTdDrrI6C7HhHEH20HtbkF+wrFWoG7Rk18TatN7L6rLcW3mV3fPZUM07W7pKEGVz01grrl/OoKHbKWAEm7n+WdRXsBJ+VinVSdI0IynquDjh6qTUM8gBrPaU3KpwSdVqdY51foQw4mr7h93Bq8W6GOkgBiosQYd2yYF5AgMBAAECgYEAk6ZyfEQmIPpPLePKB45tdT2slqF8+K+4UiRRvBDoS45a2x7UVEoza510cBItDfBighfaehDWpkUO3m3jLza0A0PHmbryNsyrw0HSLiPJVaf1/wP2jvI94veXIlL1ttyKsq1kSVhgUuRS2anq/IXdaF2G1NE/rmeGatCPg0kcp7ECQQDinjgD1z8D0y+7duL8HD3+sCL94VXlGrj2DrzI75zXyuf4CBqSSRSX1lfy3HHjw5alVnmUqVQPyIK/P50JWJxlAkEA4m9iy1czgowK7DTUWOGXMVobzZdjvS168FllIJxrj3dhAvoT6MduotUoZ3e+2e9F3AoGiLGh4+NM7lmciNCthQJAb33uCx2z9/rHyhA0nlp6uboIjSV2r+/UveIESUtxu80tER9Ti1L/GbCvtmF0et04WYHQ8S3ZmKZKhTMT3IxG7QJAMoRBGTZaIozVvv7GRRIHJhaBT2uv77ueytJW0mCzSm2KshxSpsMfdqpxeivn4z0Ta9S8wnBGus2yExbg6lEL9QJBAJG8lqFuPfpzHtoCd7Xbv0GWJ+K/modlWPWbxANJQw9OL2sqQ2Was/Ipflyggu+gDDkkhePe77YkSdlAJxVNbzY=";
    public final String MER_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIckeCP8k3Q66yOgux4RxB9tB7W5BfsKxVqBu0ZNfE2rTey+qy3Ft5ld3z2VDNO1u6ShBlc9NYK65fzqCh2ylgBJu5/lnUV7ASflYp1UnSNCMp6rg44eqk1DPIAaz2lNyqcEnVanWOdX6EMOJq+4fdwavFuhjpIAYqLEGHdsmBeQIDAQAB";
    public final String INTER_KEY = "930A7CE165CB2D27F62A34862A397618";
    public final String API_VERSION = "V1.0";
    public final String OMS_TEST_URL = "http://192.168.40.85:8907/clientapi/clientBusiness/common";
    public final String funCode = "6033";
    public final int providerId = 30481;

    public RequestMessage rm = new RequestMessage();//公共参数
    public UserSignQueryReqModel proSignQuery = new UserSignQueryReqModel();
    /**api参数结束**/

    /**--------————————————http参数开始——————————————————**/
    public final String HTTP_TEST_URL = "https://testgateway.serviceshare.com/bosskg-api-webapp/api/enterprise/baiwei-api-h5/commitSign";
    public String HTTP_TEST_RESDATA ="{\"idcardFront\":\"http://test-file.serviceshare.com/group1/M00/03/F7/wKgoVGXdPaeAFn-SAAl4Q_LOSjU651.png\",\"idcardBack\":\"http://test-file.serviceshare.com/group1/M00/03/F8/wKgoVGXdPbuAX0cQAAl4Q_LOSjU350.png\",\"userName\":\"苏利一\",\"idcardNo\":\"542522199707309014\",\"aliPay\":\"支付宝\",\"receiveBankNo\":\"13574902352\",\"userMobile\":\"13574902352\"}\n";
    public H5UserSignDTO h5UserSign;
    /**--------————————————http参数结束——————————————————**/


    static String idCard = new IDCard().getIDCard();
    static String phone = new PhoneNumber().getPhone();
    static String name = new RandInfo().randFamilyName();


    public String runBWUserSignserviceJ(JavaSamplerContext javaSamplerContext) throws Exception {


        //生成guid码 理论上不重复
        Long id = SnowflakeIdGeneratorFactory.getInstance().nextId();
        rm.setReqId("test" + id);
//        rm.setReqId("test" + System.currentTimeMillis() + 1);
        rm.setFunCode(funCode);
        rm.setMerId(MER_ID);
        rm.setVersion(API_VERSION);


        /**--------————————————业务请求参数开始获取h5签约页面——————————————————**/
        proSignQuery.setMerUserId("test" + id);
        proSignQuery.setProviderId(providerId);
        /**--------————————————业务请求参数结束——————————————————**/


        String json = JsonUtils.toJson(proSignQuery);
        //System.out.println("请求未加密数据:" + json);

        //加密处理
        byte[] bs = DESUtils.encrypt(json.getBytes(CHARSET_UTF_8), INTER_KEY);
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, MER_PRIVATE_KEY));

        String reqStr = JsonUtils.toJson(rm);
        //System.out.println("请求数据：" + reqStr);

        //发送api请求
        String resData = HttpClientHelper.sendPost(OMS_TEST_URL, "application/json", reqStr, "POST", CHARSET_UTF_8);
        //System.out.println("API响应数据：" + resData);


        //返回信息：DES 解密
        ResponseMessage responseMessage = JSON.parseObject(resData, ResponseMessage.class);
        /** 2、Base64解编码 resDATA对象解密 **/
        String respData = responseMessage.getResData();
        byte[] base64bs = Base64Utils.decode(respData.getBytes());
        /** 3、DES 解密 **/
        String resultData = DESUtils.decryptToString(base64bs, MerInfoConstant.INTER_KEY);
        responseMessage.setResData(resultData);
        //System.out.println("API解密结果："+responseMessage);



        /**--------————————————http请求参数开始——————————————————**/
        h5UserSign = new H5UserSignDTO();
        h5UserSign.setIdcardBack(javaSamplerContext.getParameter("idcardBack"));
        h5UserSign.setIdcardFront(javaSamplerContext.getParameter("idcardFront"));
        h5UserSign.setIdcardNo(javaSamplerContext.getParameter("idcardNo"));
        h5UserSign.setReceiveBankNo(javaSamplerContext.getParameter("receiveBankNo"));
        h5UserSign.setUserMobile(javaSamplerContext.getParameter("userMobile"));
        h5UserSign.setUserName(javaSamplerContext.getParameter("userName"));
        HTTP_TEST_RESDATA = JsonUtils.toJson(h5UserSign);
        //System.out.println("http请求参数：" + HTTP_TEST_RESDATA);
        /**--------————————————http请求参数结束——————————————————**/

        //提交信息：内部http接口
        String resData1 = HttpClientHelper.sendPost3(HTTP_TEST_URL, "application/json", HTTP_TEST_RESDATA, "POST", CHARSET_UTF_8,resultData);
        //System.out.println("HTTP响应数据：" + resData1);
        return resData1;
    }


    //设置可用参数及的默认值；
    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("idcardBack", "http://test-file.serviceshare.com/group1/M00/03/F8/wKgoVGXdPbuAX0cQAAl4Q_LOSjU350.png");//设置参数，签约者姓名
        params.addArgument("idcardFront", "http://test-file.serviceshare.com/group1/M00/03/F8/wKgoVGXdPbuAX0cQAAl4Q_LOSjU350.png");//设置参数，签约者收款卡号
        params.addArgument("idcardNo", idCard);//设置参数，签约者身份证号
        params.addArgument("receiveBankNo",phone );//设置参数，签约者支付宝号（手机号）
        params.addArgument("userMobile", phone);//设置参数，签约者手机号
        params.addArgument("userName", name);//设置参数，签约者姓名
        return params;
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {

        SampleResult sampleResult = new SampleResult();
        String resData = "";
        try {
            sampleResult.sampleStart();
            resData = runBWUserSignserviceJ(javaSamplerContext);
            System.out.println(resData);
            //断言
            JSONObject jsonObject1 = JSONObject.parseObject(resData);
            if (jsonObject1.get("code").equals(200)) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true + " " + jsonObject1.get("message"), SampleResult.TEXT);
                System.out.println("百威h5签约提交结果:" + true  + " " + jsonObject1.get("message"));
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + " " + jsonObject1.get("message"), SampleResult.TEXT);
                System.out.println("百威h5签约提交结果:" + false + " " + jsonObject1.get("message"));
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(resData, SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());
        } finally {
            sampleResult.sampleEnd();
        }

        return sampleResult;
    }


    public static void main(String[] args) {
        try {
            //调试
            Arguments params = new Arguments();
            params.addArgument("idcardBack", "http://test-file.serviceshare.com/group1/M00/03/F8/wKgoVGXdPbuAX0cQAAl4Q_LOSjU350.png");//设置参数，签约者姓名
            params.addArgument("idcardFront", "http://test-file.serviceshare.com/group1/M00/03/F8/wKgoVGXdPbuAX0cQAAl4Q_LOSjU350.png");//设置参数，签约者收款卡号
            params.addArgument("idcardNo", idCard);//设置参数，签约者身份证号
            params.addArgument("receiveBankNo",phone );//设置参数，签约者支付宝号（手机号）
            params.addArgument("userMobile", phone);//设置参数，签约者手机号
            params.addArgument("userName", name);//设置参数，签约者姓名
            JavaSamplerContext x = new JavaSamplerContext(params);
            BWUserSignserviceJ bwUserSignserviceJ = new BWUserSignserviceJ();
            bwUserSignserviceJ.runTest(x);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
